// 商户扣款-还款对账
app.controller('checkrefund', ['$scope', 'commonFactory', 'toaster', '$timeout', '$modal', function ($scope, commonFactory, toaster, $timeout, $modal) {
    $scope.checkrefund = {
        Index: 1,
        Size: 10,
        UsrCustId: '',
        HOrdId: '',
        StartTime: '',
        EndTime: '',
        ResultType: 0,
        IsSuccess: '-1'
    }
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.checkrefund.Index >= $scope.page) {//判断最大页码
                $scope.checkrefund.Index = $scope.page;
                return false;
            }
            $scope.checkrefund.Index++
            requesthfList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.checkrefund.Index <= 1) {//判断最小页码
            $scope.checkrefund.Index = 1;
            return false;
        }
        $scope.checkrefund.Index--;
        requesthfList()
    }
    // 查询
    $scope.search = function () {
        $scope.graybtn = true;
        $scope.checkrefund.Index = 1;
        if (!$scope.checkrefund.StartTime) {//处理时间NaN
            $scope.checkrefund.StartTime = ''
        } else {
            $scope.checkrefund.StartTime = commonFactory.DateFormat($scope.checkrefund.StartTime, 1)
        }
        if (!$scope.checkrefund.EndTime) {
            $scope.checkrefund.EndTime = ''
        } else {
            $scope.checkrefund.EndTime = commonFactory.DateFormat($scope.checkrefund.EndTime, 1)
        }
        requesthfList();
    }
    var requesthfList = function () {
        commonFactory.loading(true)
        commonFactory.ajax("/api/Reconciliation/QueryRepaymentResultList", $scope.checkrefund, function (result) {
            commonFactory.loading(false)
            $scope.graybtn = false;
            if (result.Total >= 0) {
                result.List.forEach(function (element) {
                    switch (element.TransStat) {
                        case "I": element.TransStat = "新建"; break;
                        case "C": element.TransStat = "失败"; break;
                        case "N": element.TransStat = "成功"; break;
                        case "F": element.TransStat = "失败"; break;
                        case "S": element.TransStat = "成功"; break;

                    }
                    switch (element.ReconciliationResultType) {
                        case 1: element.Type = "汇付多单"; break;
                        case 2: element.Type = "金额不一致"; break;
                        case 3: element.Type = "平台多单"; break;
                        case 4: element.Type = "平台多单不在同一天"; break;
                        case 5: element.Type = "两边数据在同一天"; break;
                        case 6: element.Type = "汇付和平台状态不一致"; break;
                    }
                });
                $scope.checkrefundList = result.List;
                $scope.checkrefundListCount = result.Total;
                $scope.page = Math.ceil(result.Total / $scope.checkrefund.Size);
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    requesthfList();
    $scope.modify = function (id, modifystatus) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
            controller: ['$scope', '$modalInstance', '$timeout', function ($scope, $modalInstance, $timeout) {
                $scope.modifyid = id;//弹出框判断
                $scope.modifystatus = modifystatus;//绑定状态
                var off = true;
                $timeout(function () {
                    $('#hideInput').focus()
                }, 100)

                $scope.ok = function () {
                    if (!off) { return false }
                    off = false;
                    commonFactory.loading(true)
                    commonFactory.ajax("/api/Reconciliation/UpdateReconciliationResult", { id: id }, function (result) {
                        commonFactory.loading(false)
                        toaster.pop('success', '成功', '修改成功');
                        $modalInstance.close();
                    }, function () {
                        commonFactory.loading(false)
                        toaster.pop('error', '失败', '后台未返回报错');
                    }, "post");
                }
                $scope.cancel = function () {
                    $modalInstance.close();
                };
                $scope.loginevent = function (e) {
                    if (e.keyCode == 13) {
                        $scope.ok();
                    }
                }
            }
            ],
            size: 'sm',
            backdrop: true,
            resolve: {

            }
        });
        modalInstance.result.then(function () {
            requesthfList();
        }, function () {
        });
    }
}]);
// 汇付多单记录
app.controller('hfreconciliation', ['$scope', 'commonFactory', 'toaster', '$timeout', '$modal', function ($scope, commonFactory, toaster, $timeout, $modal) {
    $scope.hfreconciliation = {
        Index: 1,
        Size: 10,
        UsrCustId: '',
        HOrdId: '',
        StartTime: '',
        EndTime: '',
        IsSuccess: '-1',
        Type: ''
    }
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.hfreconciliation.Index >= $scope.page) {//判断最大页码
                $scope.hfreconciliation.Index = $scope.page;
                return false;
            }
            $scope.hfreconciliation.Index++
            requesthfList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.hfreconciliation.Index <= 1) {//判断最小页码
            $scope.hfreconciliation.Index = 1;
            return false;
        }
        $scope.hfreconciliation.Index--;
        requesthfList()
    }
    // 查询
    $scope.search = function () {
        $scope.graybtn = true;
        $scope.hfreconciliation.Index = 1;
        if (!$scope.hfreconciliation.StartTime) {//处理时间NaN
            $scope.hfreconciliation.StartTime = ''
        } else {
            $scope.hfreconciliation.StartTime = commonFactory.DateFormat($scope.hfreconciliation.StartTime, 1)
        }
        if (!$scope.hfreconciliation.EndTime) {
            $scope.hfreconciliation.EndTime = ''
        } else {
            $scope.hfreconciliation.EndTime = commonFactory.DateFormat($scope.hfreconciliation.EndTime, 1)
        }
        requesthfList();
    }
    var requesthfList = function () {
        commonFactory.loading(true)
        commonFactory.ajax("/api/Reconciliation/QueryHMoreList", $scope.hfreconciliation, function (result) {
            commonFactory.loading(false)
            $scope.graybtn = false;
            if(result.Code==1){
                if(result.List){
                    result.List.forEach(function (element) {
                        switch (element.TransStat) {
                            case "I": element.TransStat = "新建"; break;
                            case "C": element.TransStat = "失败"; break;
                            case "N": element.TransStat = "成功"; break;
                            case "F": element.TransStat = "失败"; break;
                            case "S": element.TransStat = "成功"; break;
                        }
                        switch (element.Type) {
                            case 1: element.Type = "充值"; break;
                            case 2: element.Type = "提现"; break;
                            case 3: element.Type = "放款"; break;
                            case 4: element.Type = "还款"; break;
                            case 5: element.Type = "商户扣款"; break;
                        }
                    });
                }
                if (result.Total >= 0) {
                    $scope.reconciliationList = result.List;
                    $scope.reconciliationListListCount = result.Total;
                    $scope.page = Math.ceil(result.Total / $scope.hfreconciliation.Size);
                }
            }else{
                toaster.pop('error', '失败', '后台返回异常');
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    requesthfList();
    $scope.modify = function (id, modifystatus) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
            controller: ['$scope', '$modalInstance', '$timeout', function ($scope, $modalInstance, $timeout) {
                $scope.modifyid = id;//弹出框判断
                $scope.modifystatus = modifystatus;//绑定状态
                var off = true;
                $timeout(function () {
                    $('#hideInput').focus()
                }, 100)

                $scope.ok = function () {
                    if (!off) { return false }
                    off = false;
                    commonFactory.loading(true)
                    commonFactory.ajax("/api/Reconciliation/UpdateReconciliationResult", { id: id }, function (result) {
                        commonFactory.loading(false)
                        toaster.pop('success', '成功', '修改成功');
                        $modalInstance.close();
                    }, function () {
                        commonFactory.loading(false)
                        toaster.pop('error', '失败', '后台未返回报错');
                    }, "post");
                }
                $scope.cancel = function () {
                    $modalInstance.close();
                };
                $scope.loginevent = function (e) {
                    if (e.keyCode == 13) {
                        $scope.ok();
                    }
                }
            }
            ],
            size: 'sm',
            backdrop: true,
            resolve: {

            }
        });
        modalInstance.result.then(function () {
            requesthfList();
        }, function () {
        });
    }
}]);
// 金额不一致记录查询
app.controller('jereconciliation', ['$scope', 'commonFactory', 'toaster', '$timeout', '$modal', function ($scope, commonFactory, toaster, $timeout, $modal) {
    $scope.jereconciliation = {
        Index: 1,
        Size: 10,
        FullName: '',
        Mobile: '',
        OrderId: '',
        HOrdId: '',
        StartTime: '',
        EndTime: '',
        IsSuccess: '-1',
        Type: '',
        UserTransferType: '0',
        RType: '1'
    }
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.jereconciliation.Index >= $scope.page) {//判断最大页码
                $scope.jereconciliation.Index = $scope.page;
                return false;
            }
            $scope.jereconciliation.Index++
            requestjeList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.jereconciliation.Index <= 1) {//判断最小页码
            $scope.jereconciliation.Index = 1;
            return false;
        }
        $scope.jereconciliation.Index--;
        requestjeList()
    }
    // 查询
    $scope.search = function () {
        $scope.graybtn = true;
        $scope.jereconciliation.Index = 1;
        if (!$scope.jereconciliation.StartTime) {//处理时间NaN
            $scope.jereconciliation.StartTime = ''
        } else {
            $scope.jereconciliation.StartTime = commonFactory.DateFormat($scope.jereconciliation.StartTime, 1)
        }
        if (!$scope.jereconciliation.EndTime) {
            $scope.jereconciliation.EndTime = ''
        } else {
            $scope.jereconciliation.EndTime = commonFactory.DateFormat($scope.jereconciliation.EndTime, 1)
        }
        requestjeList();
    }
    var requestjeList = function () {
        commonFactory.loading(true)
        commonFactory.ajax("/api/Reconciliation/QueryResultList", $scope.jereconciliation, function (result) {
            commonFactory.loading(false)
            $scope.graybtn = false;
            if(result.Code==1){
                if(result.List){
                    result.List.forEach(function (element) {
                        switch (element.TransStat) {
                            case "I": element.TransStat = "新建"; break;
                            case "C": element.TransStat = "失败"; break;
                            case "N": element.TransStat = "成功"; break;
                            case "F": element.TransStat = "失败"; break;
                            case "S": element.TransStat = "成功"; break;

                        }
                        switch (element.Type) {
                            case 1: element.Type = "充值"; break;
                            case 2: element.Type = "提现"; break;
                            case 3: element.Type = "放款"; break;
                            case 4: element.Type = "还款"; break;
                            case 5: element.Type = "商户扣款"; break;
                        }
                        switch (element.UserTransferType) {
                            case 2: element.UserTransferType = "充值"; break;
                            case 3: element.UserTransferType = "提现"; break;
                            case 6: element.UserTransferType = "借款人收款"; break;
                            case 8: element.UserTransferType = "转账"; break;
                            case 9: element.UserTransferType = "红包"; break;
                            case 12: element.UserTransferType = "活期借款人收款"; break;
                            case 14: element.UserTransferType = "活期发放收益"; break;
                            case 18: element.UserTransferType = "网银充值"; break;
                            case 19: element.UserTransferType = "快速提现"; break;
                            case 20: element.UserTransferType = "活期提现"; break;
                            case 21: element.UserTransferType = "奖金"; break;
                        }
                    });
                }

                if (result.Total >= 0) {
                    $scope.reconciliationList = result.List;
                    $scope.reconciliationListListCount = result.Total;
                    $scope.page = Math.ceil(result.Total / $scope.jereconciliation.Size);
                }
            }else{
                toaster.pop('error', '失败', '后台返回异常');
            }

        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    requestjeList();
    $scope.modify = function (id, modifystatus) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
            controller: ['$scope', '$modalInstance', '$timeout', function ($scope, $modalInstance, $timeout) {
                $scope.modifyid = id;//弹出框判断
                $scope.modifystatus = modifystatus;//绑定状态
                var off = true;
                $timeout(function () {
                    $('#hideInput').focus()
                }, 100)

                $scope.ok = function () {
                    if (!off) { return false }
                    off = false;
                    commonFactory.loading(true)
                    commonFactory.ajax("/api/Reconciliation/UpdateReconciliationResult", { id: id }, function (result) {
                        commonFactory.loading(false)
                        toaster.pop('success', '成功', '修改成功');
                        $modalInstance.close();
                    }, function () {
                        commonFactory.loading(false)
                        toaster.pop('error', '失败', '后台未返回报错');
                    }, "post");
                }
                $scope.cancel = function () {
                    $modalInstance.close();
                };
                $scope.loginevent = function (e) {
                    if (e.keyCode == 13) {
                        $scope.ok();
                    }
                }
            }
            ],
            size: 'sm',
            backdrop: true,
            resolve: {

            }
        });
        modalInstance.result.then(function () {
            requestjeList();
        }, function () {
        });
    }
}]);
// 平台多单记录查询
app.controller('ptreconciliation', ['$scope', 'commonFactory', 'toaster', '$timeout', '$modal', function ($scope, commonFactory, toaster, $timeout, $modal) {
    $scope.ptreconciliation = {
        Index: 1,
        Size: 10,
        FullName: '',
        Mobile: '',
        OrderId: '',
        HOrdId: '',
        StartTime: '',
        EndTime: '',
        IsSuccess: '-1',
        UserTransferType: '0',
        RType: '2',
        Type:''
    }
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.ptreconciliation.Index >= $scope.page) {//判断最大页码
                $scope.ptreconciliation.Index = $scope.page;
                return false;
            }
            $scope.ptreconciliation.Index++
            requestjeList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.ptreconciliation.Index <= 1) {//判断最小页码
            $scope.ptreconciliation.Index = 1;
            return false;
        }
        $scope.ptreconciliation.Index--;
        requestjeList()
    }
    // 查询
    $scope.search = function () {
        $scope.graybtn = true;
        $scope.ptreconciliation.Index = 1;
        if (!$scope.ptreconciliation.StartTime) {//处理时间NaN
            $scope.ptreconciliation.StartTime = ''
        } else {
            $scope.ptreconciliation.StartTime = commonFactory.DateFormat($scope.ptreconciliation.StartTime, 1)
        }
        if (!$scope.ptreconciliation.EndTime) {
            $scope.ptreconciliation.EndTime = ''
        } else {
            $scope.ptreconciliation.EndTime = commonFactory.DateFormat($scope.ptreconciliation.EndTime, 1)
        }
        requestjeList();
    }
    var requestjeList = function () {
        commonFactory.loading(true)
        commonFactory.ajax("/api/Reconciliation/QueryResultList", $scope.ptreconciliation, function (result) {
            commonFactory.loading(false)
            $scope.graybtn = false;
            if (result.Code == 1) {
                if(result.List){
                    result.List.forEach(function (element) {
                        switch (element.UserTransferStatus) {
                            case 1: element.UserTransferStatus = "新建"; break;
                            case 2: element.UserTransferStatus = "成功"; break;
                            case 3: element.UserTransferStatus = "失败"; break;
                            case 4: element.UserTransferStatus = "申请成功"; break;

                        }
                        switch (element.Type) {
                            case 1: element.Type = "充值"; break;
                            case 2: element.Type = "提现"; break;
                            case 3: element.Type = "放款"; break;
                            case 4: element.Type = "还款"; break;
                            case 5: element.Type = "商户扣款"; break;
                        }
                        switch (element.UserTransferType) {
                            case 2: element.UserTransferType = "充值"; break;
                            case 3: element.UserTransferType = "提现"; break;
                            case 6: element.UserTransferType = "借款人收款"; break;
                            case 8: element.UserTransferType = "转账"; break;
                            case 9: element.UserTransferType = "红包"; break;
                            case 12: element.UserTransferType = "活期借款人收款"; break;
                            case 14: element.UserTransferType = "活期发放收益"; break;
                            case 18: element.UserTransferType = "网银充值"; break;
                            case 19: element.UserTransferType = "快速提现"; break;
                            case 20: element.UserTransferType = "活期提现"; break;
                        }
                    });    
                }

                if (result.Total >= 0) {
                    $scope.reconciliationList = result.List;
                    $scope.reconciliationListListCount = result.Total;
                    $scope.page = Math.ceil(result.Total / $scope.ptreconciliation.Size);
                }
            }else{
                toaster.pop('error', '失败', '后台返回异常');
            }
        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    requestjeList();
    $scope.modify = function (id, modifystatus) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
            controller: ['$scope', '$modalInstance', '$timeout', function ($scope, $modalInstance, $timeout) {
                $scope.modifyid = id;//弹出框判断
                $scope.modifystatus = modifystatus;//绑定状态
                var off = true;
                $timeout(function () {
                    $('#hideInput').focus()
                }, 100)

                $scope.ok = function () {
                    if (!off) { return false }
                    off = false;
                    commonFactory.loading(true)
                    commonFactory.ajax("/api/Reconciliation/UpdateReconciliationResult", { id: id }, function (result) {
                        commonFactory.loading(false)
                        toaster.pop('success', '成功', '修改成功');
                        $modalInstance.close();
                    }, function () {
                        commonFactory.loading(false)
                        toaster.pop('error', '失败', '后台未返回报错');
                    }, "post");
                }
                $scope.cancel = function () {
                    $modalInstance.close();
                };
                $scope.loginevent = function (e) {
                    if (e.keyCode == 13) {
                        $scope.ok();
                    }
                }
            }
            ],
            size: 'sm',
            backdrop: true,
            resolve: {

            }
        });
        modalInstance.result.then(function () {
            requestjeList();
        }, function () {
        });
    }
}]);
// 汇付-平台对账
app.controller('pthfreconciliation', ['$scope', 'commonFactory', 'toaster', '$timeout', '$modal', function ($scope, commonFactory, toaster, $timeout, $modal) {
    $scope.pthfreconciliation = {
        Index: 1,
        Size: 10,
        Time: '',
        Type: '0'
    }
    //下一页
    $scope.next = function () {
        if ($scope.page) {
            if ($scope.pthfreconciliation.Index >= $scope.page) {//判断最大页码
                $scope.pthfreconciliation.Index = $scope.page;
                return false;
            }
            $scope.pthfreconciliation.Index++
            requestjeList();
        }
    }
    //上一页
    $scope.prev = function () {
        if ($scope.pthfreconciliation.Index <= 1) {//判断最小页码
            $scope.pthfreconciliation.Index = 1;
            return false;
        }
        $scope.pthfreconciliation.Index--;
        requestjeList()
    }
    // 查询
    $scope.search = function () {
        $scope.graybtn = true;
        $scope.pthfreconciliation.Index = 1;
        if (!$scope.pthfreconciliation.Time) {//处理时间NaN
            $scope.pthfreconciliation.Time = ''
        } else {
            $scope.pthfreconciliation.Time = commonFactory.DateFormat($scope.pthfreconciliation.Time, 1)
        }
        requestjeList();
    }
    var checkinfo = function(){
        if(!$scope.pthfreconciliation.Time){
            toaster.pop('warning', '提示', '请选择对账时间');
            return false;
        }
        if($scope.pthfreconciliation.Type<=0){
            toaster.pop('warning', '提示', '请选择具体对账类型');
            return false;
        }
        return true;
    }
    var requestjeList = function () {
        $scope.graybtn = true;
        commonFactory.loading(true)
        commonFactory.ajax("/api/Reconciliation/QueryList", $scope.pthfreconciliation, function (result) {
            commonFactory.loading(false)
            $scope.graybtn = false;
            if(result.Code==1){
                result.List.forEach(function (element) {
                    switch (element.Type) {
                        case 1: element.Type = "充值"; break;
                        case 2: element.Type = "提现"; break;
                        case 3: element.Type = "放款"; break;
                        case 4: element.Type = "还款"; break;
                        case 5: element.Type = "商户扣款"; break;
                    }
                });

                if (result.Total >= 0) {
                    $scope.reconciliationList = result.List;
                    $scope.reconciliationListListCount = result.Total;
                    $scope.page = Math.ceil(result.Total / $scope.pthfreconciliation.Size);
                }
            }else{
                toaster.pop('error', '失败', '后台返回异常');
            }

        }, function () {
            commonFactory.loading(false)
            $scope.graybtn = false;
        }, "post");
    }
    requestjeList();
    $scope.modify = function (id, modifystatus) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/model/delPmodel.html?v=' + Math.random(),
            controller: ['$scope', '$modalInstance', '$timeout', function ($scope, $modalInstance, $timeout) {
                $scope.modifyid = id;//弹出框判断
                $scope.modifystatus = modifystatus;//绑定状态
                var off = true;
                $timeout(function () {
                    $('#hideInput').focus()
                }, 100)

                $scope.ok = function () {
                    if (!off) { return false }
                    off = false;
                    commonFactory.loading(true)
                    commonFactory.ajax("/api/Reconciliation/UpdateReconciliationResult", { id: id }, function (result) {
                        commonFactory.loading(false)
                        toaster.pop('success', '成功', '修改成功');
                        $modalInstance.close();
                    }, function () {
                        commonFactory.loading(false)
                        toaster.pop('error', '失败', '后台未返回报错');
                    }, "post");
                }
                $scope.cancel = function () {
                    $modalInstance.close();
                };
                $scope.loginevent = function (e) {
                    if (e.keyCode == 13) {
                        $scope.ok();
                    }
                }
            }
            ],
            size: 'sm',
            backdrop: true,
            resolve: {

            }
        });
        modalInstance.result.then(function () {
            requestjeList();
        }, function () {
        });
    }
    // 添加
    $scope.addreconciliation = function () {
        if (checkinfo()) {
            $scope.graybtn = true;
            if (!$scope.pthfreconciliation.Time) {//处理时间NaN
                $scope.pthfreconciliation.Time = ''
            } else {
                $scope.pthfreconciliation.Time = commonFactory.DateFormat($scope.pthfreconciliation.Time, 1)
            }
            commonFactory.ajax("/api/Reconciliation/Save", {PlanDate:$scope.pthfreconciliation.Time,Type:$scope.pthfreconciliation.Type}, function (result) {
                commonFactory.loading(false)
                requestjeList()
                if (result.Code==1) {
                    toaster.pop('success', '成功', '已确认，对账工作将于次日凌晨进行');
                }else{
                    toaster.pop('warning', '警告', result.Data);
                }
            }, function () {
                commonFactory.loading(false)
                $scope.graybtn = false;
                toaster.pop('error', '失败', '后台未返回报错');
            }, "post");
        }
    }
}]);